﻿CREATE PROCEDURE [dbo].[TempInventory_UpdateIds]
	@RangeReviewId int
AS

--ArticleIds
Update TempInventories
Set ArticleId = case when a.Id is not null then a.Id else 0 end
From TempInventories t
LEFT JOIN Articles a on t.ArticleCode = a.Code and a.RangeReviewId = @RangeReviewId

--StoreIds
Update TempInventories
Set StoreId = case when s.Id is not null then s.Id else 0 end
From TempInventories t
LEFT JOIN Stores s on t.StoreCode = s.Code and s.CountryId = (Select CountryId From RangeReviews Where Id = @RangeReviewId)


--CategoryIds
Update TempInventories
Set CategoryId = case when cat.Id is not null then cat.Id else 0 end
From TempInventories t
Left join Categories cat on cat.Code = t.CategoryCode 